package com.gzit;

import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.gzit.adapter.CustomerListAdapter;
import com.gzit.view.tag.TagGroupView;

public class TagDemo extends Activity {

	TagGroupView tagGroupView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// CrazyView view = new CrazyView(this);
		// this.addContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT,
		// LayoutParams.WRAP_CONTENT));

		// TagDataAdapter adapter = new TagDataAdapter();
		// adapter.(Arrays.asList(new TagItem[] { new TagItem("牛人"),
		// new TagItem("猪八戒背媳妇"), new TagItem("鸟居笼中，恨关羽不能张飞") }));
		//
		// tagGroupView = (TagGroupView) this.findViewById(R.id.tag);
		// tagGroupView.setAdapter(adapter);
		// tagGroupView.invalidate();

		// final FlipIndicatorView flipIndicator = (FlipIndicatorView) this
		// .findViewById(R.id.flip_indicator);
		// flipIndicator.setIndicatorNumber(3);
		//
		// this.findViewById(R.id.prev).setOnClickListener(new OnClickListener()
		// {
		//
		// @Override
		// public void onClick(View v) {
		// flipIndicator.showPrev();
		//
		// }
		// });
		//
		// this.findViewById(R.id.next).setOnClickListener(new OnClickListener()
		// {
		//
		// @Override
		// public void onClick(View v) {
		// flipIndicator.showNext();
		//
		// }
		// });

		GridView view = (GridView) this.findViewById(R.id.grid);
		ImageAdapter adapter = new ImageAdapter(this, R.layout.image_item);
		view.setAdapter(adapter);

		List<ImageInfo> bitmaps = queryImage();
		adapter.reload(bitmaps);

		// queryVideo();
		// queryAudio();

	}

	private void queryAudio() {

		Cursor cursor = this.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.Media._ID,
						MediaStore.Audio.Media.DISPLAY_NAME,
						MediaStore.Audio.Media.TITLE,
						MediaStore.Audio.Media.DURATION,
						MediaStore.Audio.Media.ARTIST,
						MediaStore.Audio.Media.ALBUM,
						MediaStore.Audio.Media.YEAR,
						MediaStore.Audio.Media.MIME_TYPE,
						MediaStore.Audio.Media.SIZE,
						MediaStore.Audio.Media.DATA },
				/*
				 * MediaStore.Audio.Media.MIME_TYPE + "=? or " +
				 * MediaStore.Audio.Media.MIME_TYPE + "=?", new String[] {
				 * "audio/mpeg", "audio/x-ms-wma" },
				 */null, null, null);

		StringBuffer sb = new StringBuffer();
		if (cursor.moveToFirst()) {
			do {
				String dName = cursor.getString(1);
				sb.append(dName);
				sb.append("\n");
			} while (cursor.moveToNext());

		}

		TextView tv = (TextView) this.findViewById(R.id.show);
		tv.setText(sb.toString());

	}

	private List<ImageInfo> queryImage() {

		//http://developer.android.com/reference/android/provider/MediaStore.Images.ImageColumns.html#BUCKET_ID
		
		List<ImageInfo> infos = new LinkedList<ImageInfo>();

		Cursor cursor = this.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Images.Media._ID,
						MediaStore.Images.Media.DISPLAY_NAME,
						MediaStore.Images.Media.TITLE,
						MediaStore.Images.Media.MIME_TYPE,
						MediaStore.Images.Media.SIZE,
						MediaStore.Images.Media.DATA },
				/*
				 * MediaStore.Audio.Media.MIME_TYPE + "=? or " +
				 * MediaStore.Audio.Media.MIME_TYPE + "=?", new String[] {
				 * "audio/mpeg", "audio/x-ms-wma" },
				 */null, null, null);

		if (cursor.moveToFirst()) {
			do {

				// 获取缩略图
				long imageId = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Images.Media._ID));

				Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
						this.getContentResolver(), imageId,
						MediaStore.Images.Thumbnails.MICRO_KIND, null);

				ImageInfo info = new ImageInfo();
				info.image = bm;

				infos.add(info);

				// //////////

				// String dName = cursor.getString(1);

				// 读到源图片...
//				Uri uri = Uri.withAppendedPath(
//						MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageId
//								+ "");
//				try {
//					Bitmap bitmap = BitmapFactory
//							.decodeStream(getContentResolver().openInputStream(
//									uri));
//
//					Log.i("test", bitmap.getWidth() + ":" + bitmap.getHeight());
//				} catch (FileNotFoundException e) {
//					e.printStackTrace();
//				}

			} while (cursor.moveToNext());

		}

		return infos;

	}

	private void queryVideo() {

		Cursor cursor = this.getContentResolver().query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Video.Media._ID,
						MediaStore.Video.Media.DISPLAY_NAME,
						MediaStore.Video.Media.TITLE,
						MediaStore.Video.Media.DURATION,
						MediaStore.Video.Media.ARTIST,
						MediaStore.Video.Media.ALBUM,
						MediaStore.Video.Media.MIME_TYPE,
						MediaStore.Video.Media.SIZE,
						MediaStore.Video.Media.DATA },
				/*
				 * MediaStore.Audio.Media.MIME_TYPE + "=? or " +
				 * MediaStore.Audio.Media.MIME_TYPE + "=?", new String[] {
				 * "audio/mpeg", "audio/x-ms-wma" },
				 */null, null, null);

		StringBuffer sb = new StringBuffer();
		if (cursor.moveToFirst()) {
			do {

				// 获取缩略图
				long videoId = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Video.Media._ID));

				Bitmap bm = MediaStore.Video.Thumbnails.getThumbnail(
						this.getContentResolver(), videoId,
						MediaStore.Images.Thumbnails.MICRO_KIND, null);

				ImageView iv = (ImageView) this.findViewById(R.id.image);
				iv.setImageBitmap(bm);
				// //////////

				String dName = cursor.getString(1);
				sb.append(dName);
				sb.append("\n");
			} while (cursor.moveToNext());

		}

		TextView tv = (TextView) this.findViewById(R.id.show);
		tv.setText(sb.toString());

	}

	class ImageInfo {
		public Bitmap image;
		public boolean checked;

	}

	public class ImageAdapter extends CustomerListAdapter<ImageInfo> {

		public ImageAdapter(Context context, int itemLayoutId) {
			super(context, itemLayoutId);
		}

		@Override
		protected void render(final ImageInfo item, View view) {

			ImageView iv = (ImageView) view.findViewById(R.id.image);
			iv.setImageBitmap(item.image);

			final CheckBox sel = (CheckBox) view.findViewById(R.id.sel);
			sel.setChecked(item.checked);

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					item.checked = !item.checked;
					sel.setChecked(item.checked);

				}
			});

		}

	}

}